package effective.thread;

import java.util.concurrent.TimeUnit;

public class StopThread2 {
	private static boolean stopRequested;

	private static synchronized void requestStop() {
		stopRequested = true;
	}

	private static synchronized boolean stopRequested() {
		return stopRequested;
	}

	public static void main(String[] args) throws InterruptedException {
		Thread backgroundThread = new Thread(new Runnable() {

			@Override
			public void run() {

				int i = 0;
				while (!stopRequested) {
					i++;
				}
				// if (!stopRequested()) {
				// while (true) {
				// i++;
				// }
				// }
			}
		});
		backgroundThread.start();
		TimeUnit.SECONDS.sleep(1);
		stopRequested = true;
	}
	// public static void main(String[] args) throws InterruptedException {
	// Thread backgroundThread = new Thread(new Runnable() {
	//
	// @Override
	// public void run() {
	//
	// int i = 0;
	// // while (!stopRequested) {
	// // i++;
	// // }
	// if (!stopRequested) {
	// while (true) {
	// i++;
	// }
	// }
	// }
	// });
	// backgroundThread.start();
	// TimeUnit.SECONDS.sleep(1);
	// stopRequested = true;
	// }

}
